<%-- 
    Document   : Konten
    Created on : Apr 22, 2012, 11:01:26 AM
    Author     : Deng
--%>

<%@page import="web.vungsi"%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="likeKomeng.js" /></script>
  <script type="text/javascript" src="dislikeKomeng.js" /></script>
<script type="text/javascript" src="hapusKomeng.js" /></script>
<script type="text/javascript" src="insertKomeng.js" /></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Halaman Konten</title>
</head>
<body>
  <h1>Ini halaman konten lho</h1>
  <div id="bukanContent">
    <%
      //ambil kue
      Cookie[] list = request.getCookies();
      Cookie username_kuki = null;
      if (list != null) {
        for (int i = 0; i < list.length; i++) {
          if ("username".equals(list[i].getName())) {
            username_kuki = list[i];
          }
        }
      }

      //ambil id postingan
      String post_id = request.getParameter("pid");
      //post_id = "2"; String username_kuki = "edwin"; //dummy
      int id_post = Integer.parseInt(post_id);
      String sementara = "";
      int temp = 0;
      vungsi pungsi = new vungsi();

      //konek ke sql
      String koneksiURL = "jdbc:mysql://localhost/progin_171_13509024?user=progin&password=progin";
      Connection koneksi = null;
      Statement statement = null;
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      koneksi = DriverManager.getConnection(koneksiURL);
      statement = koneksi.createStatement();

      //ambil tabel post
      ResultSet ambilTebelPost = statement.executeQuery("SELECT * FROM tbl_post WHERE id=" + id_post);
      ambilTebelPost.next();
      int author_id = (int) ambilTebelPost.getInt("author_id");
      String title = ambilTebelPost.getString("title");
      String konten = ambilTebelPost.getString("content");
      String create_time = ambilTebelPost.getString("create_time");
      int id = (int) ambilTebelPost.getInt("id");
      int like_count = ambilTebelPost.getInt("like_count");
      int dislike_count = ambilTebelPost.getInt("dislike_count");

      //untuk hitung jumlah komeng
      ResultSet ambilPostComment = statement.executeQuery("SELECT COUNT(create_time) FROM tbl_comment WHERE post_id=" + id);
      ambilPostComment.next();
      int jumlahKomeng = (int) ambilPostComment.getInt("COUNT(create_time)");

      //ambil tabel user
      ResultSet ambilTebelUser = statement.executeQuery("SELECT * FROM tbl_user WHERE id=" + author_id);
      ambilTebelUser.next();
      String username = ambilTebelUser.getString("username");
      int user_id = ambilTebelUser.getInt("id");

      //untuk waktu komentar
      ResultSet ambilTebelKomen = statement.executeQuery("SELECT * FROM tbl_comment WHERE post_id=" + id);
      //ambilTebelKomen.next();
      String createTime = "";

      //untuk daftar komentar
      ResultSet ambilUserComment = statement.executeQuery("SELECT * FROM tbl_comment JOIN tbl_user ON tbl_user.id = tbl_comment.user_id WHERE post_id=" + id + " ORDER BY create_time DESC");

      out.write("<strong>Judul Konten</strong></br>" + title + "</br></br>");
      out.write("<strong>Nama Lengkap Pengepost</strong></br>" + "<a href=profile.jsp class=header>" + username + "</a></br></br>");
      sementara = vungsi.ambilKonten(konten);
      out.write("<strong>Konten</strong><br>" + sementara + "</br></br>");
      String waktuSekarang = vungsi.dateFormat.format(vungsi.date);
      String selisihWaktu = vungsi.DateDiff(create_time, waktuSekarang);
      out.write("<strong>Waktu Konten Di-Post</strong></br>" + selisihWaktu + "</br></br>");
      out.write("<strong>Jumlah Komentar</br></strong>" + jumlahKomeng + "</br></br>");
      //daftar komentar
      out.write("<div id=penampungan></div>");
      while (ambilUserComment.next() != false) {
        sementara = ambilUserComment.getString("username");
        temp = ambilUserComment.getInt("id");
        out.write("<div id=komen" + temp + "><strong>Nama Komentator : </strong>" + sementara + "</br>");
        out.write("<strong>Avatar Komentator :</strong> <img src=upload/avatar/" + sementara + " width='20px' height='20px'/></br>");

        createTime = ambilUserComment.getString("create_time");
        selisihWaktu = vungsi.DateDiff(createTime, waktuSekarang);
        out.write("<strong>Waktu Komentar : </strong>" + selisihWaktu + "</br>");
        sementara = ambilUserComment.getString("content");
        out.write("<strong>Komentar : </strong>" + sementara + "<br>");
        if (username_kuki.getValue().equalsIgnoreCase(username)) {
          out.write("<input type='button' value='Hapus' onClick='gaEnak(" + temp + ")' id='hapus" + temp + "' />");
        }
        out.write("</div></br>");
      }
      if (username_kuki != null) {
        out.write("<textarea rows='10' cols='40' id='teksarea'></textarea></br>");
        out.write("<input type='button' value='Tambah Komentar' onClick='Enak(" + post_id + "," + user_id + ")' /><br><br>");
      }
      out.write("<strong>Jumlah Like/Dislike</strong><br>");
      out.write("Jumlah Like : " + like_count + "<br>");
      out.write("Jumlah Dislike : " + dislike_count + "<br>");
      if (username_kuki != null) {
        out.write("<input type='button' value='Like' OnClick='gaEngga(" + post_id + ", " + author_id + ")' /><input type='button' value='Dislike' OnClick='Engga(" + post_id + ", " + author_id + ")' /><br><br>");
      }
    %>
  </div>
</body>
</html>
